package com.fz.song.web.mq;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.fz.song.web.entity.User;
import com.fz.song.web.service.MqService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "test-topic-group")
@Slf4j
public class Consumer implements RocketMQListener<MessageExt> {


    @Autowired
    private MqService mqService;


    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("接收到mq消息:{}", JSONUtil.toJsonStr(messageExt));
        String msgId = messageExt.getMsgId();
        byte[] body = messageExt.getBody();
        String msg = new String(body);
        User user = JSONUtil.toBean(msg, User.class);
        mqService.saveUser(user, msgId);
    }
}
